package com.choudou5.solr.dataimport.config;


import com.choudou5.solr.dataimport.constants.DataImportConstant;

import java.util.List;

/**
 * DataImport 管理器
 * @author choudou5
 * @create 2017-08-下午 8:18
 **/
public class DIManager {

    private static List<DIEntity> entitys = null;

    public static List<DIEntity> loadSetting(String configPath){
        entitys = DIXMLTools.parserXML(configPath);
        return entitys;
    }


    /**
     * 获取 实体
     * @param module
     * @return
     */
    public static DIEntity getEntity(String module){
        for (DIEntity entity : entitys) {
            if(entity.getIndexName().equals(module)){
                return entity;
            }
        }
        return null;
    }

    public static boolean existEntity(String module){
        DIEntity entity = getEntity(module);
        return entity!=null?true:false;
    }


    /**
     * 获取 查询
     * @param module
     * @param importType
     * @return
     */
    public static DIQuery getQuery(String module, DataImportConstant.ImportType importType){
        for (DIEntity entity : entitys) {
            if(entity.getIndexName().equals(module)){
                if(DataImportConstant.ImportType.FULL.equals(importType)){
                    return entity.getQueryPlugin().getFull();
                }else if(DataImportConstant.ImportType.DELTA.equals(importType)){
                    return entity.getQueryPlugin().getDelta();
                }
            }
        }
        return null;
    }

    public static DIDataRange getDataRange(long total, int macNo, int macTotal){
        long avg = total%macTotal==0 ? total/macTotal : total/macTotal+1;
        long start = avg * (macNo - 1);
        long end = avg * macNo;
        System.out.println("total:"+total+", macNo:"+macNo+", macTotal:"+macTotal+", range:"+start+" - "+ end);
        return new DIDataRange(total, start, end);
    }

    public static void main(String[] args) {
        long total = 1462001;
        int macTotal = 3;
        for (int i = 1; i <= macTotal; i++) {
            getDataRange(total, i, macTotal);
        }
    }


}
